var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var hasMore = true;
var goinMore = true;
var page = 1;
var isSubmitEvent = false;
Page({

  /**
     * 页面的初始数据
     */
  data: {
    uid: 0,
    token: 0,
    openid: 0,
    userinfo: {},
    followUserList:[],

    hasMingPian: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
      var cata = (parseInt(res.usercata) > 0) ? true : false;
      that.setData({
        uid: res.uid,
        token: res.token,
        openid: res.wxsmall_openid,
        hasMingPian: cata
      });
      }
    });

    app.GetUserInfo(that.data.uid, function (res) {
      that.setData({
        userinfo: res
      });
    });

    this.getFollowlist(1);
  },

  goAtherPage: function (e) {
    if (isSubmitEvent) {
      console.log("isSubmitEvent");
      isSubmitEvent = false;
      return;
    }
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({ url: url })
    }
  },

  loadMore: function () {
    if (hasMore && goinMore) {
      goinMore = false;
      page++;
      this.getFollowlist(page);
    } else {
      console.log("没有更多了");
    }
  },

  //获取申请交换名片的用户列表
  getFollowlist: function (cpage) {
    var that = this;
    util.api({
      method: "GET",
      data: {
        ac: "myfollowlist",
        uid: that.data.uid,
        page: cpage,
        num: 20,
        publickey: util.publiccode
      },
      success: function (responseData) {
        console.log("myfollowlist ", responseData);
        if (responseData && responseData.statusCode == 200) {
          var result = responseData.data;
          if (result && result.length > 0) {
            for (var i = 0; i < result.length; i++) {
              var date = new Date(result[i].ctime * 1000);
              result[i].formatTime = util.formatTime(date, '-', ':');
            }
            
            that.setData({
              followUserList: that.data.followUserList.concat(result),
            });
          } else {
            hasMore = false;
          }
          goinMore = true;
        }

      }
    });
  },

  refreshData:function(tuid, rel){
    for(var i = 0; i < this.data.followUserList.length; i++) {
      var listData = this.data.followUserList;
      var item = listData[i];
      if(tuid == item.uid) {
        item.rel = rel;
        this.setData({
          followUserList: listData
        })
      }
    }
  },

  agreeAction: function(e) {
    isSubmitEvent = true;
    var that = this;
    console.log("formId ", e.detail.formId);
    app.saveFormId(e.detail.formId);//保存fromId

    var ouid = this.data.uid;
    var tuid = e.detail.value.uid;
    var token = this.data.token;
    var privatekey, ac;
    var isFriend = e.detail.value.friend;
    if (isFriend == 1 || isFriend == 2) {
      privatekey = utilMd5.hexMD5(ouid + 'delaction' + token);
      ac = "delaction";
    } else {
      // if (!that.data.userInfo.usercata) {
      //   that.setData({
      //     createMP_show: true
      //   })
      //   return;
      // }
      privatekey = utilMd5.hexMD5(ouid + 'addaction' + token);
      ac = "addaction";
    }
    util.api({
      data: {
        publickey: util.publiccode,
        ac: ac,
        uid: ouid,
        tuid: tuid,
        privatekey: privatekey
      },
      method: 'POST',
      success: function (responseData) {
        var data = responseData.data;
        if (data.res === true) {
          if (isFriend == 1 || isFriend == 2) {
            app.dialog('取消成功', 'success');
            that.refreshData(tuid, 3);
          } else {
            app.dialog('交换成功', 'success');
            that.refreshData(tuid, 1);
          }
        } else {
          if (that.data.isFriend == 1 || that.data.isFriend == 2) {
            app.dialog('取消失败', 'loading');
          } else {
            app.dialog('交换成功', 'loading');
          }
        }
      }
    });
  },

})